package com.qxzn.module.mine.activity;

import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.alibaba.fastjson.JSONObject;
import com.qxzn.app.bean.BaseBean;
import com.qxzn.app.page.BaseActivity;
import com.qxzn.app.page.interfaces.MyInterface;
import com.qxzn.app.page.model.LoginAndRegisterModel;
import com.qxzn.app.page.util.Util;
import com.qxzn.app.util.ActivityStack;
import com.qxzn.app.util.AppUtil;
import com.qxzn.app.util.CommonUtil;
import com.qxzn.app.util.SharePreferenceUtil;
import com.qxzn.module.mine.R;
import com.qxzn.module.mine.R2;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 更新密码
 */
public class UpdatePwdActivity extends BaseActivity implements MyInterface.UpdatePwdListener {

    @BindView(R2.id.et_pwd_old)
    EditText etPwdOld;
    @BindView(R2.id.et_pwd_new)
    EditText etPwdNew;
    @BindView(R2.id.et_pwd_repeat)
    EditText etPwdRepeat;
    @BindView(R2.id.iv_pwd_old_eye)
    ImageView ivPwdOldEye;
    @BindView(R2.id.iv_pwd_new_eye)
    ImageView ivPwdNewEye;
    @BindView(R2.id.iv_pwd_repeat_eye)
    ImageView ivPwdRepeatEye;
    private LoginAndRegisterModel loginAndRegisterModel;
    private boolean isShowOld;
    private boolean isShowNew;
    private boolean isShowNewRepeat;

    @Override
    protected int initLayout() {
        return R.layout.activity_update_pwd;
    }

    @Override
    protected void initView() {
        Util.setPasswordFilter(etPwdOld);
        Util.setPasswordFilter(etPwdNew);
        Util.setPasswordFilter(etPwdRepeat);
    }

    @Override
    protected void iniTitleBar() {
        tv_title.setText("账号密码更改");
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        loginAndRegisterModel = new LoginAndRegisterModel(this);
        loginAndRegisterModel.setUpdatePwdListener(this);
    }

    @OnClick({R2.id.iv_pwd_old_eye, R2.id.iv_pwd_new_eye, R2.id.iv_pwd_repeat_eye, R2.id.tv_confirm})
    public void onViewClicked(View view) {
        int i = view.getId();
        if (i == R.id.iv_pwd_old_eye) {
            isShowOld = Util.setHintOrShowPwdGray(etPwdOld, ivPwdOldEye, isShowOld);
        } else if (i == R.id.iv_pwd_new_eye) {
            isShowNew = Util.setHintOrShowPwdGray(etPwdNew, ivPwdNewEye, isShowNew);
        } else if (i == R.id.iv_pwd_repeat_eye) {
            isShowNewRepeat = Util.setHintOrShowPwdGray(etPwdRepeat, ivPwdRepeatEye, isShowNewRepeat);
        } else if (i == R.id.tv_confirm) {
            if (CommonUtil.isFastClick()) {
                return;
            }
            changePwd();
        }
    }

    private void changePwd() {
        if (!CommonUtil.isLettersAndNumbers(etPwdOld.getText().toString().trim())) {
            AppUtil.showToast(this, "旧密码不正确，请重新输入");
            return;
        }
        if (!CommonUtil.isLettersAndNumbers(etPwdNew.getText().toString().trim())) {
            AppUtil.showToast(this, getString(R.string.invalid_password_string));
            return;
        }
        if (!CommonUtil.isLettersAndNumbers(etPwdRepeat.getText().toString().trim())) {
            AppUtil.showToast(this,  getString(R.string.invalid_password_string));
            return;
        }
        if (!etPwdNew.getText().toString().trim().equals(etPwdRepeat.getText().toString().trim())) {
            AppUtil.showToast(this, "两次密码不一致");
            return;
        }
        showProgress();
        JSONObject params = new JSONObject();
        params.put("type", 1);
        params.put("username", SharePreferenceUtil.getUserName(this));
        params.put("old_password", etPwdOld.getText().toString().trim());
        params.put("new_password", etPwdNew.getText().toString().trim());
        loginAndRegisterModel.updatePwd(params);
    }

    @Override
    public void onUpdatePwdSuccess(BaseBean baseBean) {
        hideProgress();
        if (baseBean != null) {
            if (baseBean.getCode() == 200) {
                AppUtil.showToast(this, "密码修改成功");
                SharePreferenceUtil.setPwd(this, etPwdNew.getText().toString().trim());
                startActivity(new Intent().setComponent(new ComponentName(this, "com.qxzn.app.personalcenter.activity.LoginByPwdActivity")));
                ActivityStack.finishAllWithoutActivity("com.qxzn.app.personalcenter.activity.LoginByPwdActivity");
                return;
            }
        }
        if (baseBean != null && !TextUtils.isEmpty(baseBean.getMsg())) {
            AppUtil.showToast(this, baseBean.getMsg());
        } else {
            AppUtil.showToast(this, "密码修改失败，请重试");
        }
    }

    @Override
    public void onUpdatePwdFailure() {
        hideProgress();
        AppUtil.showToast(this, "网络错误，请稍后再试！");
    }
}
